package com.mifengs.commonbase.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.mifengs.commonbase.base.BaseApplication;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class NetWorkUtils {

	/**
	 * 检查当前网络是否可用
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isNetworkAvailable(BaseApplication context) {

		// 获取手机所有连接管理对象（包括对wi-fi,net等连接的管理）
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		if (connectivityManager == null) {
			return false;
		} else {
			// 获取NetworkInfo对象
			NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

			if (networkInfo != null && networkInfo.length > 0) {
				for (int i = 0; i < networkInfo.length; i++) {
					System.out.println(i + "===状态===" + networkInfo[i].getState());
					System.out.println(i + "===类型===" + networkInfo[i].getTypeName());
					// 判断当前网络状态是否为连接状态
					if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}
	
	/**
	 * 获取当前ip地址
	 * 
	 * @param context
	 * @return
	 */
//	public static String getLocalIpAddress(Context context) {
//		try {
//			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {  
//                NetworkInterface intf = en.nextElement();    
//                   for (Enumeration<InetAddress> enumIpAddr = intf    
//                           .getInetAddresses(); enumIpAddr.hasMoreElements();) {    
//                       InetAddress inetAddress = enumIpAddr.nextElement();    
//                       if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {    
//                             
//                           return inetAddress.getHostAddress().toString();    
//                       }    
//                   }    
//            }  
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return null;
//	}
	
	public static String getWifiLocalIpAddress(Context context) throws SocketException {
	       /* WifiManager wifi = (WifiManager) context
	                .getSystemService(Context.WIFI_SERVICE);
	        WifiInfo info = wifi.getConnectionInfo();
	        int ipAddress = info.getIpAddress();
	        return intToIp(ipAddress);*/
	    	  for(Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();){
	    	        NetworkInterface intf = en.nextElement();
	    	        for(Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();){
	    	            InetAddress inetAddress = enumIpAddr.nextElement();
	    	            if(!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)){
	    	                return inetAddress.getHostAddress().toString(); 
	    	            } 
	    	        }

	    	    } 
	    	    return "null";
	    }


	/**
	 * 将ip的整数形式转换成ip形式
	 * 
	 * @param ipInt
	 * @return
	 */
	public static String intToIp(int ipInt) {
		StringBuilder sb = new StringBuilder();
		sb.append(ipInt & 0xFF).append(".");
		sb.append((ipInt >> 8) & 0xFF).append(".");
		sb.append((ipInt >> 16) & 0xFF).append(".");
		sb.append((ipInt >> 24) & 0xFF);
		return sb.toString();
	}
}